<?php

namespace App\Http\Controllers;
use App\Repositories\BLL\Wechat\EventBLL;
use App\Repositories\BLL\Wechat\MessageBLL;

use Log;

class WechatController extends Controller
{
	private $eventBLL,$messageBLL;
	public function __construct(EventBLL $event,MessageBLL $message){
		$this->eventBLL = $event;
		$this->messageBLL = $message;
	}
	

    /**
     * 处理微信的请求消息
     *
     * @return string
     */
    public function serve()
    {
        $wechat = app('wechat');
        $wechat->server->setMessageHandler(function($message){
            switch ($message->MsgType) {
		        case 'event':
					$result = $this->eventBLL->receiveEvent($message);
		            return $result;
		            break;
		        case 'text':
					$result = $this->messageBLL->receiveText($message);
					if($result == "CustomerService"){
						return new \EasyWeChat\Message\Transfer();
					}
		            return $result;
		            break;
		        default:
		            return '';
		            break;
		    }
        });

        return $wechat->server->serve();
    }
}
